package com.emergency.embackend.controller;

import com.emergency.embackend.common.R;
import com.emergency.embackend.dto.UserDTO;
import com.emergency.embackend.entity.User;
import com.emergency.embackend.param.UserParam;
import com.emergency.embackend.param.UserPasswordParam;
import com.emergency.embackend.service.UserService;
import com.emergency.embackend.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chddd
 * @description
 * @create 2024/1/20 14:56:38
 */
@RestController
@Slf4j
@RequestMapping("/user")
@EnableAsync
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody UserParam userParam){
        String res = userService.login(userParam);
        if("输入的验证码有误".equals(res)){
            return R.error(res);
        }
        return R.ok().put("token",res);
    }

    @PostMapping("register")
    public R register(@RequestBody UserParam userParam){
       boolean flag = userService.register(userParam);
        if(!flag){
            return R.error("用户已存在,无须注册!!");
        }
        return R.ok("用户创建成功");
    }

    @GetMapping("info")
    public R info(@RequestParam("id") Long id ) {
        if(id == null){
            return R.error("参数为空!!");
        }
        User user = userService.info(id);
        if(user == null){
            R.error("用户不存在");
        }
        return R.ok().put("data",user);
    }

    @GetMapping("code")
    public R code(@RequestParam("phone") String phone) {
        //保存验证码到redis
        String code = userService.saveCode(phone);
        log.info("验证码：{}",code);
        //发送验证码异步实现
        userService.sendCode(phone,code);
        return R.ok();
    }

    @GetMapping("/me")
    public R me(){
        UserDTO user = UserHolder.getUser();
        return R.ok().put("data",user);
    }

    @PostMapping("/update")
    public R update(@RequestBody UserParam userParam, HttpServletRequest request){
        if(userParam == null){
            return R.error("参数异常，请联系管理员");
        }
        userService.update(userParam,request);
        return R.ok();
    }
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UserPasswordParam userParam){
        if(userParam == null){
            return R.error("参数异常，请联系管理员");
        }
        userService.updatePassword(userParam);
        return R.ok("修改成功");
    }
    @GetMapping("/bindingPhone")
    public R bindingPhone(@RequestParam("phone")String phone,@RequestParam("code") String code,HttpServletRequest request){
        Long userId = UserHolder.getUser().getId();
        return R.ok().put("data",userService.bindingPhone(phone,userId,code,request));
    }
    @GetMapping("logout")
    public R logout(){
        UserHolder.removeUser();
        return R.ok();
    }
}
